package com.example.zhunongzonghe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhunongzonghe.beans.ZnMessageBean;
import com.example.zhunongzonghe.dao.ZnMessageMapper;
import com.example.zhunongzonghe.dao.ZnUsersMapper;
import com.example.zhunongzonghe.entity.ZnMessage;
import com.example.zhunongzonghe.entity.ZnUsers;
import com.example.zhunongzonghe.service.ZnMessageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 蔡某人
* @description 针对表【zn_message】的数据库操作Service实现
* @createDate 2024-04-04 21:40:48
*/
@Service
public class ZnMessageServiceImpl extends ServiceImpl<ZnMessageMapper, ZnMessage>
    implements ZnMessageService{
    @Autowired
    ZnMessageMapper znMessageMapper;
    @Autowired
    ZnUsersMapper znUsersMapper;

    /**
     * 分页查询全部资讯
     * @param currentPage
     * @param pageNum
     * @return
     */
    @Override
    public Page<ZnMessageBean> searchMessageAll(Integer currentPage, Integer pageNum,Object searchInfo) {
        Page<ZnMessage> postPage = new Page<>(currentPage,pageNum);
        LambdaQueryWrapper<ZnMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(ZnMessage::getMessTitle,searchInfo)
                .or().like(ZnMessage::getMessContent,searchInfo);
        Page<ZnMessage> znMessagePage = znMessageMapper.selectPage(postPage,queryWrapper);
        List<ZnMessageBean> beanList = new ArrayList<>();
        for (ZnMessage znMessage: znMessagePage.getRecords()
        ) {
            ZnMessageBean znMessageBean = new ZnMessageBean();
            ZnUsers usersBean = znUsersMapper.selectById(znMessage.getCreateBy());
            znMessageBean.setNickName(usersBean.getNickname());
            BeanUtils.copyProperties(znMessage,znMessageBean);
            beanList.add(znMessageBean);
        }
        Page<ZnMessageBean> beanPage = new Page<>(currentPage,pageNum, znMessagePage.getTotal());
        beanPage.setRecords(beanList);
        return beanPage;
    }


}




